﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ContinuousMacro.CApCMPartValidator
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.ContinuousMacro.Resources;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;
using Tecnomatix.Planning;


namespace DnProcessSimulateCommands.ContinuousMacro;

internal class CApCMPartValidator : ITxValidator
{
  public ITxObject GetObject(string str)
  {
    TxObjectList txObjectList = (TxObjectList) null;
    if (!string.IsNullOrEmpty(str))
      txObjectList = TxApplication.ActiveDocument.GetObjectsByName(str);
    if (txObjectList != null && ((Collection<ITxObject>) txObjectList).Count > 0)
    {
      foreach (ITxObject itxObject in (Collection<ITxObject>) txObjectList)
      {
        if (this.IsValidObject(itxObject))
          return itxObject;
      }
    }
    return (ITxObject) null;
  }

  public string GetText(ITxObject obj) => obj == null ? string.Empty : obj.Name;

  public bool IsValidObject(ITxObject obj) => this.IsValidObject(obj, out string _);

  public bool IsValidObject(ITxObject obj, out string errorMessage)
  {
    errorMessage = ContinuousMacroResources.OBJECT_SELECTION_ERROR;
    if (obj != null && obj is ITxProcessModelObject)
    {
      ITxPlanningObject planningRepresentation = (obj as ITxProcessModelObject).PlanningRepresentation;
      if (planningRepresentation != null && (planningRepresentation.PlanningObjectType == null || planningRepresentation.PlanningObjectType == 1))
        return true;
    }
    return false;
  }

  public bool IsValidText(string text, out string errorMessage)
  {
    return this.IsValidObject(this.GetObject(text), out errorMessage);
  }
}
